package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.MemberAttrValueManager;
import com.fish.anne.model.MemberAttrValue;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class MemberAttrValueAction extends BaseAction implements Preparable {
    private MemberAttrValueManager memberAttrValueManager;
    private List memberAttrValues;
    private MemberAttrValue memberAttrValue;
    private Long  maId;

    public void setMemberAttrValueManager(MemberAttrValueManager memberAttrValueManager) {
        this.memberAttrValueManager = memberAttrValueManager;
    }

    public List getMemberAttrValues() {
        return memberAttrValues;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String memberAttrValueId = getRequest().getParameter("memberAttrValue.maId");
            if (memberAttrValueId != null && !memberAttrValueId.equals("")) {
                memberAttrValue = memberAttrValueManager.get(new Long(memberAttrValueId));
            }
        }
    }

    public String list() {
        memberAttrValues = memberAttrValueManager.getAll();
        return SUCCESS;
    }

    public void setMaId(Long  maId) {
        this. maId =  maId;
    }

    public MemberAttrValue getMemberAttrValue() {
        return memberAttrValue;
    }

    public void setMemberAttrValue(MemberAttrValue memberAttrValue) {
        this.memberAttrValue = memberAttrValue;
    }

    public String delete() {
        memberAttrValueManager.remove(memberAttrValue.getMaId());
        saveMessage(getText("memberAttrValue.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (maId != null) {
            memberAttrValue = memberAttrValueManager.get(maId);
        } else {
            memberAttrValue = new MemberAttrValue();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (memberAttrValue.getMaId() == null);

        memberAttrValueManager.save(memberAttrValue);

        String key = (isNew) ? "memberAttrValue.added" : "memberAttrValue.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}